package com.ge.modules.api.secondController;

import com.ge.common.annotation.AuthIgnoreApi;
import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.annotation.LoginTechnicianApi;
import com.ge.common.utils.DateUtils;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.entity.TbOrderEntity;
import com.ge.modules.entity.TbStoreEntity;
import com.ge.modules.entity.TbTechnicianEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 技师订单
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-11 17:28:43
 */
@RestController
@RequestMapping("/api/technician/order")
public class TbTechnicianOrderController {

    @Autowired
    private TbOrderService tbOrderService;

    /**
     * 显示订单列表(可根据不同状态显示不同订单)
     *
     * @param tbTechnicianEntity
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginTechnicianApi TbTechnicianEntity tbTechnicianEntity, @RequestParam Map<String, Object> params) {
        if (tbTechnicianEntity == null) {
            return R.error(501, "登录失效");
        }
        params.put("commodityMasterId", tbTechnicianEntity.getTechnicianId());
        //查询列表数据
        Query query = new Query(params);
        List<TbOrderEntity> tbOrderEntityList = tbOrderService.queryList(query);
        int total = tbOrderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbOrderEntityList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询订单详细信息
     */
    @RequestMapping("/info/{orderCode}")
    public R info(@LoginTechnicianApi TbTechnicianEntity tbTechnicianEntity, @PathVariable("orderCode") String orderCode) {
        if (tbTechnicianEntity == null) {
            return R.error(501, "登录失效");
        }
        //通过对象存储条件
        TbOrderEntity tbOrderEntity = new TbOrderEntity();
        tbOrderEntity.setOrderCode(orderCode);
        tbOrderEntity.setCommodityMasterId(tbTechnicianEntity.getTechnicianId());
        try {
            TbOrderEntity tbOrderEntity1 = tbOrderService.queryObject(tbOrderEntity);
            return R.ok().put("data", tbOrderEntity1);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    /**
     * 批量删除 做状态修改
     *
     * @param orderCodes 订单编号数组
     * @return
     */
    @RequestMapping("/deleteList")
    public R deleteList(@RequestBody Integer[] orderCodes) {
        tbOrderService.updateBatch(orderCodes);
        return R.ok();
    }


    /**
     * 根据时间统计 订单数量和总价
     *
     * @param tbTechnicianEntity
     * @param timeSign
     * @return COUNT(id) orders,SUM(order_price) totalPrice
     */
    @GetMapping("/countOrders")
    public R countOrders(@LoginTechnicianApi TbTechnicianEntity tbTechnicianEntity, String timeSign) {
        Map<String, Object> map = new HashMap<>();
        map.put("commodityMasterId", tbTechnicianEntity.getTechnicianId());
        //统计已完成的订单 5
        map.put("orderStatus", 5);
        //判断时间标识是不否是查询当天订单的数据 非空就添加时间查询
        timeSign = "2";
        if (!timeSign.equals("1")) {
            //得到年月日的时间字符串
            String dates = DateUtils.dateToString(new Date(), DateUtils.DATE_PATTERN);
            //在拼接一天的开始时间和结束时间
            String startDates = dates + " 00:00:00";
            String endDates = dates + " 23:59:59";
            map.put("startTime", DateUtils.toDate(startDates, DateUtils.DATE_TIME_PATTERN));
            map.put("endTime", DateUtils.toDate(endDates, DateUtils.DATE_TIME_PATTERN));
        }
        Map m = tbOrderService.queryOrdersAndTotal(map);
        return R.ok().put("data", m);
    }

}
